using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
using System.Text.RegularExpressions;

namespace DAL
{
    public class ChargeDAL
    {
        public static int Save(ChargeInfo info)
        {
            string sql = string.Format("insert into [charge]([UserID],[Money],[CreateDate]) values(@UserID,@Money,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(ChargeInfo info)
        {
            string sql = string.Format("update [charge] set [Money]=@Money where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }

        public static List<ChargeInfo> FindPage(int userid, string keyword, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = "";
            if (userid != 0) 
            {
                filter += " and [charge].UserID="+userid;
            }
            if (B.MyAdmin.Role == 2)
            {
                filter += " and [charge].UserID in (select ID from [user] where AdminID="+B.MyAdmin.ID+")";
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                if (Regex.IsMatch(keyword, @"^[+-]?\d*$"))
                {
                    filter += " and [charge].UserID = " + keyword;
                }
                else
                {
                    filter += " and ([charge].UserID in( select ID from [user] where NickName like '%" + keyword + "%'))";
                }
            }
            DataTable dt = CommonDAL.FindCPage(startindex, pagesize, order, desc, filter, "[charge_page]", out count);
            return ListSimpleData(dt);
        }
        public static ChargeInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[charge]");
            return SimpleData(dr);
        }
        public static List<ChargeInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[charge]");
            return ListSimpleData(dt);
        }

        public static List<ChargeInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [charge]"));
            return ListSimpleData(dt);
        }
        private static ChargeInfo SimpleData(DataRow dr)
        {
            ChargeInfo info = new ChargeInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Money = DataConvert.ToFloat(dr["Money"]) / 100;
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.UserName = DataConvert.ToString(dr["NickName"]);
            }
            return info;
        }
        private static ChargeInfo AllData(DataRow dr)
        {
            ChargeInfo info = new ChargeInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Money = DataConvert.ToFloat(dr["Money"])/100;
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
            }
            return info;
        }
        private static List<ChargeInfo> ListSimpleData(DataTable dt)
        {
            List<ChargeInfo> list = new List<ChargeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<ChargeInfo> ListAllData(DataTable dt)
        {
            List<ChargeInfo> list = new List<ChargeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

